package vn.orderonline.web.admin.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.orderonline.com.core.business.dto.OrderBillDto;
import vn.orderonline.com.core.business.dto.ToOrderDto;
import vn.orderonline.com.core.business.toorder.IDrinkBasket;
import vn.orderonline.com.core.business.toorder.IFoodBasket;
import vn.orderonline.com.core.business.toorder.IToOrder;

@Controller
@RequestMapping("/toorder")
public class ToOrderController {
	
	private static final Logger LOGGER = Logger
			.getLogger(ToOrderController.class);
	
	@Autowired
	private IToOrder iToOrder;
	@Autowired
	private IFoodBasket iFoodBasket;
	@Autowired
	private IDrinkBasket iDrinkBasket;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
	public String allOrder(ModelMap model) {
		LOGGER.info("START TO GET ALL BILL ORDER!");
		try {
			model.addAttribute("list", iToOrder.getAllBillOrder());
			model.addAttribute("order", new ToOrderDto());
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO GET ALL BILL ORDER!");
		return "/orderbill/search-order-bill";
	}
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String detail(ModelMap model, @RequestParam String orderCode) {
		LOGGER.info("START TO VIEW DETAIL OF ORDER!");
		try {
			model.addAttribute("order", iToOrder.getOrder(orderCode));
			Integer orderId = iToOrder.getOrderIdByOrderCode(orderCode);
			model.addAttribute("listFood", iFoodBasket.getAllByOrderId(orderId));
			model.addAttribute("listDrink", iDrinkBasket.getAllByOrderId(orderId));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO VIEW DETAIL OF ORDER!");
		return "/orderbill/detail-order-bill";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String search(ModelMap model, @ModelAttribute OrderBillDto dto) {
		LOGGER.info("START TO SEARCH ORDER BILL!");
		try {
			model.addAttribute("list", iToOrder.search(dto));
			model.addAttribute("order", dto);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO SEARCH ORDER BILL!");
		return "/orderbill/search-order-bill";
	}

}
